#include "TaskExecutor.h"

#include "Task.h"
#include "UnitArbitrator.h"
#include "BuildEventTimeline.h"
#include "ReservedMap.h"
#include "..\Util.h"

namespace BroodWar
{
	namespace Addon
	{
		TaskExecutor::TaskExecutor(BWSAL::TaskExecutor* executor)
		{
			instance = executor;
		}
		
		TaskExecutor::~TaskExecutor()
		{
			BWSAL::TaskExecutor::destroy();
		}
		
		TaskExecutor::!TaskExecutor()
		{
			BWSAL::TaskExecutor::destroy();
		}

		void TaskExecutor::Create(IUnitArbitrator^ arbitrator, IBuildEventTimeline^ timeline, IReservedMap^ reserveMap, IBuildingPlacer^ defaultBuildingPlacer)
		{
			UnitArbitrator^ a = DirectCast<UnitArbitrator^>(arbitrator);
			BuildEventTimeline^ t = DirectCast<BuildEventTimeline^>(timeline);
			ReservedMap^ r = DirectCast<ReservedMap^>(reserveMap);
			BWSAL::BuildingPlacer* bp = GetBuildingPlacer(defaultBuildingPlacer);
			BWSAL::TaskExecutor::destroy();
			s_instance = gcnew TaskExecutor(BWSAL::TaskExecutor::create(a->instance, t->instance, r->instance, bp));
		}

		TaskExecutor^ TaskExecutor::Instance::get()
		{
			return s_instance;
		}

		void TaskExecutor::OnOffer(IEnumerable<Unit^>^ units)
		{
			instance->onOffer(ToSet<Unit^, BWAPI::Unit*>(units, &Api::ConvertUnit));
		}
		
		void TaskExecutor::OnRevoke(Unit^ unit, double bid)
		{
			instance->onRevoke(ConvertUnit(unit), bid);
		}
		
		String^ TaskExecutor::Name::get()
		{
			return gcnew String(instance->getName().c_str());
		}
		
		void TaskExecutor::OnFrame()
		{
			instance->onFrame();
		}
		
		void TaskExecutor::Run(Task^ task)
		{
			instance->run(ConvertTask(task));
		}
		
		int TaskExecutor::GetRunningCount(BuildType type)
		{
			return instance->getRunningCount(ConvertBuild(type));
		}
	}
}

/*
#pragma once
#include <BWSAL/Arbitrator.h>
#include <BWSAL/BuildType.h>
#include <BWAPI.h>
namespace BWSAL
{
  class Task;
  class BuildingPlacer;
  class TaskScheduler;
  class ReservedMap;
  class BuildEventTimeline;
  class TaskExecutor : public UnitController, public BWAPI::AIModule
  {
    public:
      static TaskExecutor* create( UnitArbitrator* arbitrator, BuildEventTimeline* timeline, ReservedMap* reserveMap, BuildingPlacer* defaultBuildingPlacer );
      static TaskExecutor* getInstance();
      static void destroy();
      virtual void onOffer( std::set< BWAPI::Unit* > units );
      virtual void onRevoke( BWAPI::Unit* unit, double bid );
      virtual std::string getName() const;
      virtual void onFrame();
      void run( Task* t );
      int getRunningCount( BuildType type ) const;
    private:
      TaskExecutor();
      ~TaskExecutor();
      void updateTask( Task* t );
      UnitArbitrator* m_arbitrator;
      ReservedMap* m_reservedMap;
      BuildingPlacer* m_defaultBuildingPlacer;
      BuildEventTimeline* m_timeline;
      static TaskExecutor* s_taskExecutor;
      std::list< Task* > m_runningTasks;

      void computeCreatedUnit( Task* t );
      void computeSecondCreatedUnit( Task* t );
      void computeIsExecuting( Task* t );
      void computeIsCompleted( Task* t );
      void computeIsReady( Task* t );
      void getReady( Task* t );
      void execute( Task* t );
      std::set< BWAPI::Unit* > m_builders;
      std::map< BuildType, int > m_runningCount;
  };
}
*/